package com.cnplan.core;

import javax.imageio.ImageIO;
import javax.print.*;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;

public class PrintImage {
    private static String fontName="长宁区城建档案室";
    private static String urlport="http://192.168.6.90:8080";
    /**
     * 画图片的方法
     *
     * @param fileName [图片的路径]
     */
    public static void drawImage(String fileName) {
        try {
            fileName = urlport+fileName;
            DocFlavor dof = null;
            //根据用户选择不同的图片格式获得不同的打印设备
            if (fileName.endsWith(".gif")) {
            //gif
                dof = DocFlavor.INPUT_STREAM.GIF;
            } else if (fileName.endsWith(".jpg")) {
            //jpg
                dof = DocFlavor.INPUT_STREAM.JPEG;
            } else if (fileName.endsWith(".png")) {
                //png
                dof = DocFlavor.INPUT_STREAM.PNG;
            }

            URL url = new URL(fileName);
            InputStream fin = url.openStream();
            BufferedImage bi = ImageIO.read(fin);

            //添加水印
            Graphics2D g=bi.createGraphics();
            g.setColor(new Color(137, 149, 226));
            g.setFont(new Font("宋体",  Font.BOLD + Font.ITALIC,90));
            g.drawString(fontName,(bi.getWidth()-(fontName.length()*90))/2,bi.getHeight()/2);
            g.dispose();

            //转为字节流
            ByteArrayOutputStream stream= new ByteArrayOutputStream();
            ImageIO.write(bi,"jpg",stream);
            byte[] byteArray=stream.toByteArray();
            InputStream inputStream =new ByteArrayInputStream(byteArray);

            //获得打印属性
            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            //每一次默认打印一页
            pras.add(new Copies(1));
            pras.add(PrintQuality.HIGH);
            pras.add(Chromaticity.MONOCHROME);
            if(bi.getWidth()>bi.getHeight()){
                pras.add(OrientationRequested.LANDSCAPE);
            }else{
                pras.add(OrientationRequested.PORTRAIT);
            }

            //获得打印设备 ，字节流方式，图片格式
            PrintService pss[] = PrintServiceLookup.lookupPrintServices(dof, pras);
            //如果没有获取打印机
            if (pss.length == 0) {
            //终止程序
                return;
            }
            //获取第一个打印机
            PrintService ps = pss[0];
            System.out.println("Printing image..........." + ps);
            //获得打印工作
            DocPrintJob job = ps.createPrintJob();
            //文档属性
            DocAttributeSet das = new HashDocAttributeSet();
            // 设置打印纸张的大小（将像素转化为mm）
            das.add(new MediaPrintableArea(0, 0, bi.getWidth(), bi.getHeight(), MediaPrintableArea.MM));
            //设置打印内容
            Doc doc = new SimpleDoc(inputStream, dof, das);
                //开始打印
            job.print(doc, pras);
            fin.close();
        } catch (Exception ie) {
            ie.printStackTrace();
            //捕获io异常
            ie.printStackTrace();
        }finally {

        }
    }

    /**
     * 主函数
     *
     * @param args
     */
    public static void main(String args[]) {
//        PrintImage dp = new PrintImage();
//        dp.drawImage("http://localhost:8081/jpg/08cc5e02fe1175856d21bf5e23b27676.jpg");
    }


}
